home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / is_con10.zip / ISCONSOL.C < prev    next >
Text File  |  1991-12-21  |  2KB  |  60 lines

  1. /*
  2.   From: Bob Jarvis
  3.     To: Jerry Coffin            Date: 17 Dec 91  21:53:34
  4.   Subj: file redirection
  5.   Conf: `PC Assembly Language'
  6.  
  7. In a message of <15 Dec 91  12:15:04>, Jerry Coffin (1:128/60) writes:
  8.  >not equal it unless they have redirected to the same file.  It would
  9.  >appear, therefore, that accurately determining whether input or output
  10.  >has been redirected requires using DOS function 52h to find the
  11.  >system file table, and tracking through it to find whether the file
  12.  >associated with the handle in question is actually connected to a file
  13.  >named CON.  ( PLEASE prove me wrong on this, though. )
  14.  
  15. Perhaps the following code will help:
  16. */
  17. /* 
  18.  * isconsole() - determines if a file handle is associated with the console.
  19.  *
  20.  *    Returns:
  21.  *       0           Handle is not associated with the console
  22.  *       non-zero    Handle is associated with the console
  23.  */
  24.  
  25. #include <dos.h>
  26.  
  27. #define IS_DEVICE          0x0080
  28. #define IS_FASTCONSOLE     0x0010
  29. #define IS_CONSOUT         0x0002
  30. #define IS_CONSIN          0x0001
  31.  
  32. int isconsole(int handle)
  33.    {
  34.    union REGS regs;
  35.  
  36.    regs.h.ah = 0x44;       /* IOCTL service                          */
  37.    regs.h.al = 0;          /* subfunction 0 - get device information */
  38.    regs.x.bx = handle;
  39.  
  40.    int86(0x21, ®s, ®s);
  41.  
  42.    if(regs.x.dx & IS_DEVICE)
  43.       if((regs.x.dx & IS_FASTCONSOLE) || 
  44.             (regs.x.dx & IS_CONSOUT)  ||
  45.             (regs.x.dx & IS_CONSIN))
  46.          return(~0);
  47.  
  48.    return(0);
  49.    }
  50.  
  51. #include <stdio.h>
  52.  
  53. main()
  54.    {
  55.    if(isconsole(fileno(stdout)))
  56.       printf("stdout is associated with the console");
  57.    else
  58.       printf("stdout is *not* associated with the console\n");
  59.    }
  60.